﻿using System;
using System.Xml.Linq;
using FirstFloor.Documents.Annotations.Storage;

namespace FirstFloor.Documents.Presentation.Annotations
{
    /// <summary>
    /// Reads and writes hyperlink annotations.
    /// </summary>
    public class HyperlinkFactory
        : TextAnnotationFactory<Hyperlink>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="HyperlinkFactory"/> class.
        /// </summary>
        public HyperlinkFactory()
            : base(Hyperlink.TypeHyperlink)
        {
        }

        /// <summary>
        /// Creates the annotation.
        /// </summary>
        /// <returns>The annotation.</returns>
        public override Hyperlink CreateAnnotation()
        {
            return new Hyperlink();
        }

        /// <summary>
        /// Reads the annotation properties.
        /// </summary>
        /// <param name="annotation">The annotation.</param>
        /// <param name="source">The source.</param>
        protected override void ReadAnnotationProperties(Hyperlink annotation, XElement source)
        {
            annotation.TargetUri = new Uri((string)source.Attribute("TargetUri"), UriKind.RelativeOrAbsolute);
        }

        /// <summary>
        /// Writes the annotation properties.
        /// </summary>
        /// <param name="annotation">The annotation.</param>
        /// <param name="target">The target.</param>
        protected override void WriteAnnotationProperties(Hyperlink annotation, XElement target)
        {
            target.Add(new XAttribute("TargetUri", annotation.TargetUri.ToString()));
        }
    }
}
